<template>
  <div class="alive"></div>
</template>

<script>
import { mapGetters, mapMutations } from "vuex";

/* TODO: Implement a local version of the AlertBlock and use that
instead of using the global state instance of the AlertBlock. */
export default {
  name: "KeepAlive",
  computed: { ...mapGetters(["api"]) },
  methods: {
    ...mapMutations(["updateErrorPrompt"]),
    checkForActiveSession(endpoint) {
      if (!endpoint) {
        this.updateErrorPrompt("No configuration.");
      }
      fetch(endpoint)
        .then(response => response.text())
        .then(data => {
          console.log(data);
          if (data !== "OK") {
            this.updateErrorPrompt(`Session expired ${new Date().toString()}`);
          }
          return "";
        })
        .catch(error => {
          this.updateErrorPrompt(`Request failed. ${error}`);
        });
    },
    startProcess(endpoint, milliseconds) {
      this.timer = setInterval(() => {
        this.checkForActiveSession(endpoint);
      }, milliseconds);
    }
  },
  mounted() {
    this.startProcess(this.api.session, 60000);
  }
};
</script>

<style scoped>
.alive {
  display: none;
}
</style>
